package com.lin.fengxiu.manager.redis;

import com.lin.fengxiu.bo.OrderMessageBO;
import com.lin.fengxiu.repository.UserCouponRepository;
import com.lin.fengxiu.service.CouponBackService;
import com.lin.fengxiu.service.OrderCancelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.lang.Nullable;

/**
 * @ClassName TopicMessageListener
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/4/13 6:57
 * @Version 1.0
 */
public class TopicMessageListener implements MessageListener {

    @Autowired
    private OrderCancelService orderCancelService;

    @Autowired
    private CouponBackService couponBackService;

    /**
     * @Description TODO 接收redis键空间通知的方法
     * @param message
     * @param bytes
     */
    @Override
    public void onMessage(Message message, @Nullable byte[] bytes) {
        byte[] body = message.getBody();
        byte[] channel = message.getChannel();

        String expiredKey = new String(body);
        String topic = new String(channel);
        System.out.println(expiredKey);
        System.out.println(topic);
        // 注入订单归还的业务接口实例
        OrderMessageBO messageBO = new OrderMessageBO(expiredKey);
        orderCancelService.cancel(messageBO);
        couponBackService.returnBack(messageBO);
    }
}
